package ferp.android.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import ferp.android.GUI;
import ferp.core.log.Log;

/**
 * User: olegs
 * Date: 2/1/14 1:30 PM
 */
public class CustomTextView extends View
{
  public CustomTextView(Context context, AttributeSet attrs)
  {
    super(context, attrs);

    paint.setTypeface(GUI.Font.BOLD);

    if (attrs != null)
    {
      // read button text
      TypedArray ta = getContext().obtainStyledAttributes(attrs, ids);

      // assign the text
      content = ta.getString(0);
      paint.setTextSize(ta.getDimensionPixelSize(1, 0));
      paint.setTextScaleX(ta.getFloat(2, 0));

      ta.recycle();
    }
  }

  public void set(String text, int color)
  {
    content = text;

    if (content != null && content.equals("Мизер"))
      Log.debug(Log.TAG, "CTV: set " + content);

    paint.setTextScaleX(1);
    paint.setColor(color);

    requestLayout();
    invalidate();
  }

  public void fitTextByWidth(float width)
  {
    if (content == null)
      return;

    float mtw = bounds.width();

    if (width < mtw)
    {
      paint.setTextScaleX(width / mtw);

      setMeasuredDimension(Math.round(width), getMeasuredHeight());
    }
  }

  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
  {
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);

    if (content != null)
    {
      // fit text by height
      paint.setTextSize(height);
      paint.getTextBounds(content, 0, content.length(), bounds);

      int w = bounds.width() + bounds.left;

      width = width == 0 ? w : Math.min(width, w);
      height = bounds.height() + bounds.bottom;
    }

    setMeasuredDimension(width, height);

    if (content != null && content.equals("Мизер"))
      Log.debug(Log.TAG, "CTV: measure " + GUI.m2s(widthMeasureSpec, heightMeasureSpec) + " -> " + width + "x" + height);
  }

  @Override
  protected void onDraw(Canvas canvas)
  {
    super.onDraw(canvas);

    if (content == null)
      return;

    paint.getTextBounds(content, 0, content.length(), bounds);
    canvas.drawText(content, 0, getMeasuredHeight() - bounds.bottom, paint);

/*
    //Log.debug(Log.TAG, "CTV: ts = " + paint.getTextSize() + ", descent=" + fm.descent + ", ascent=" + fm.ascent + ", top=" + fm.top + ", bottom=" + fm.bottom + ", leading=" + fm.leading);

    if (content.equals("Мизер"))
    {
      Log.debug(Log.TAG, "CTV: draw '" + content + "' bounds=" + bounds + ", height=" + bounds.height());
      Log.debug(Log.TAG, "--------- " + getMeasuredWidth() + "x" + getMeasuredHeight() + ", bl=" + bl);
    }
*/
  }

  private static final int[] ids = new int[]{android.R.attr.text, android.R.attr.textSize, android.R.attr.textScaleX};

  private String content;
  private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  private final Rect bounds = new Rect();
}